package com.wanmait.movies.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;

import com.wanmait.movies.util.DBHelper;
import com.wanmait.movies.vo.User;

public class UserDao {
	private UserDao() {}
	private static UserDao userDao = new UserDao();
	public static UserDao getObj() {
		return userDao;
	}

	public User getUser(int userId) {
		User ui = new User();
		Connection co = DBHelper.getDBHelper().getconn();
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = " select * from `user` where  id=? ";
		
		try {
			ps = co.prepareStatement(sql);
			ps.setInt(1, userId);
			rs = ps.executeQuery();
			//�ȶ����ݿ����Ƿ����˺�������ͬ��һ�����ݣ�����У���ôrs.next()����true
			if (rs.next()) {
				 ui.setId(rs.getInt("id"));
				 ui.setUserBirthday(rs.getTimestamp("user_birthday"));
				 ui.setUserFaceimage(rs.getString("user_faceimage"));
				 ui.setUserMail(rs.getString("user_mail"));
				 ui.setUserName(rs.getString("user_name"));
				 ui.setUserPassword(rs.getString("user_password"));
				 ui.setUserPhone(rs.getString("user_phone"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			DBHelper.getDBHelper().close(rs, ps, co);
		}
		return ui;
	}
	
	//��¼
	public User getUserInfo(String username, String userpass) {
		User ui = new User();
		Connection co = DBHelper.getDBHelper().getconn();
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = " select * from `user` where user_name=? and user_password=? ";
		
		try {
			ps = co.prepareStatement(sql);
			ps.setString(1, username);
			ps.setString(2, userpass);
			rs = ps.executeQuery();
			//�ȶ����ݿ����Ƿ����˺�������ͬ��һ�����ݣ�����У���ôrs.next()����true
			if (rs.next()) {
				 ui.setId(rs.getInt("id"));
				 ui.setUserBirthday(rs.getTimestamp("user_birthday"));
				 ui.setUserFaceimage(rs.getString("user_faceimage"));
				 ui.setUserMail(rs.getString("user_mail"));
				 ui.setUserName(rs.getString("user_name"));
				 ui.setUserPassword(rs.getString("user_password"));
				 ui.setUserPhone(rs.getString("user_phone"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			DBHelper.getDBHelper().close(rs, ps, co);
		}
		return ui;
	}
	//����û����Ƿ��ظ�
	public boolean checkUsername(String username) {
		boolean b = true; 
		Connection co = DBHelper.getDBHelper().getconn();
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = " select user_name from `user` where user_name=?";
		
		try {
			ps = co.prepareStatement(sql);
			ps.setString(1, username);
			rs = ps.executeQuery();
			b = rs.next(); 
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			DBHelper.getDBHelper().close(rs, ps, co);
		}
		return b;//����û����ظ�������һ��true����֮false
	}
	//ע�����û�
	public void register(String name,String password,String phonenum) {
		  
		Connection co = DBHelper.getDBHelper().getconn();
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = " insert into user(user_name, user_password, user_phone) value(?, ?, ?)";
		
		try {
			ps = co.prepareStatement(sql);
			ps.setString(1, name);
			ps.setString(2, password);
			ps.setString(3, phonenum);
			ps.executeUpdate(); 
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			DBHelper.getDBHelper().close(rs, ps, co);
		}
		 
	}
	//�޸ĸ�����Ϣ
	public void rewriteInfo(String phone, String email, String birth, int id) {
		/*Connection co = DBHelper.getDBHelper().getconn();
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = " UPDATE user SET user_phone=? , user_mail=?, user_birthday=? WHERE id=? ";
		
		try {
			ps = co.prepareStatement(sql);
			ps.setString(1, phone);
			ps.setString(2, email);
			ps.setTimestamp(3, Timestamp.valueOf(birth));
			ps.setInt(4, id);
			ps.executeUpdate(); 
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			DBHelper.getDBHelper().close(rs, ps, co);
		}*/
		String sql = " UPDATE user SET user_phone=? , user_mail=?, user_birthday=? WHERE id=? ";
		SimpleDateFormat sdFormat = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
		String b = birth + " 00:00:00";
		DBHelper.getDBHelper().update(sql, phone, email, Timestamp.valueOf(b), id);
	}

	
	public void rewriteInfo(String phone, String email, String birth, int id,String image) {
		Connection co = DBHelper.getDBHelper().getconn();
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = " UPDATE user SET user_phone=? , user_mail=?, user_birthday=?, user_faceimage=? WHERE id=?";
		
		try {
			ps = co.prepareStatement(sql);
			ps.setString(1, phone);
			ps.setString(2, email);
			ps.setString(3, birth);
			ps.setString(4, image);
			ps.setInt(5, id);
			ps.executeUpdate(); 
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			DBHelper.getDBHelper().close(rs, ps, co);
		}
	}
}
